/**
 * 加解密的工具类
 * 使用：https://github.com/JuneAndGreen/sm-crypto
 *
 * @author wangbuhao
 */
import { sm2, sm3, sm4 } from 'sm-crypto';

const cipherMode = 1; // 1 - C1C3C2，0 - C1C2C3，默认为1
const publicKey = '';
const privateKey = '7d0a5a1d3f1a9d83329bdcab606f80a63906722cfe17f4b72efc3bc8d20bdf88';
const key = '';

/**
 * 国密加解密工具类
 */
export default {
	// SM2加密
	doSm2Encrypt(msgString) {
		return sm2.doEncrypt(msgString, publicKey, cipherMode);
	},
	// SM2解密
	doSm2Decrypt(encryptData) {
		return sm2.doDecrypt(encryptData, privateKey, cipherMode);
	},
	// SM2数组加密
	doSm2ArrayEncrypt(msgString) {
		return sm2.doEncrypt(msgString, publicKey, cipherMode);
	},
	// SM2数组解密
	doSm2ArrayDecrypt(encryptData) {
		return sm2.doDecrypt(encryptData, privateKey, cipherMode, { output: 'array' });
	},
	// SM3哈希
	doSm3Hash(msgString) {
		return sm3(msgString);
	},
	// SM4 加密
	doSm4Encrypt(msgString) {
		return sm4.encrypt(msgString, key);
	},
	// SM4 CBC加密
	doSm4CbcEncrypt(msgString) {
		return sm4.encrypt(msgString, key, { mode: 'cbc', iv: 'fedcba98765432100123456789abcdef' });
	},
	// SM4 解密
	doSm4Decrypt(encryptData) {
		return sm4.decrypt(encryptData, key);
	},
	// SM4 CBC解密
	doSm4CbcDecrypt(encryptData) {
		return sm4.decrypt(encryptData, key, { mode: 'cbc', iv: 'fedcba98765432100123456789abcdef' });
	},
};
